<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>后台管理日志</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <link rel="stylesheet" href="css/main.css" media="all">
</head>
<body>
<script type="text/html" id="admin">
    {{d.admin.name}}
</script>
<script src="layui/layui.js"></script>
<script src="js/jquery-1.12.4.js"></script>
<!--工具栏-->
<table id="logAdminTable" lay-filter="logAdminTableFilter"></table>
<script type="text/html" id="tb1">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="tb2">
    <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="dels">批量删除</a>
</script>
<script>
    layui.use(['table', 'laydate', 'layer'], function () {
        var table = layui.table;
        var layer = layui.layer;

        table.render({
            elem: '#logAdminTable',
            url: 'logAdmin/show',
            page: true,
            limit: 10,
            limits: [20, 50, 200],
            toolbar: '#tb2',
            cols: [[
                {type: 'checkbox'}
                , {title: '编号', field: 'id'}
                , {title: '管理员', templet: '#admin'}
                , {title: '操作', field: 'logContent'}
                , {title: '类型', field: 'logType'}
                , {title: '日期', field: 'logDate'}
                , {title: 'IP 地址', field: 'ip'}
                , {title: '操作', toolbar: '#tb1'}
            ]]
        });
        // 批量删除
        table.on('toolbar(logAdminTableFilter)',function (obj) {
            var status = table.checkStatus(obj.config.id);
           if (obj.event == 'dels'){
                var data = status.data;
                if (data.length == 0){
                    layer.msg('请至少勾选一项！');
                    return;
                }else {
                    var ids = [];
                    for (var i = 0; i<data.length; i++){
                        ids.push(data[i].id);
                    }
                }

                $.ajax({
                    url: 'logAdmin/del',
                    dataType: 'json',
                    data: 'id=' + ids,
                    success:function (result) {
                        if (result.isDelete){
                            layer.msg('删除成功！');
                            table.reload('logAdminTable');
                        }else {
                            layer.msg('删除失败！');
                            table.reload('logAdminTable');
                        }
                    }
                })
            }
        });
        // 绑定单个删除事件

        table.on('tool(logAdminTableFilter)', function (obj) {
            var data = obj.data;
            var id = obj.data.id;
            console.log(obj);
            if (obj.event == 'del') {
                layer.confirm('确认要删除？', function (index) {
                    $.ajax({
                        url: 'logAdmin/del',
                        dataType: 'json',
                        data: 'id=' + id,
                        success: function (result) {
                            if (result.isDelete) {
                                obj.del();
                                layer.close(index);
                                layer.msg("删除成功！");
                            } else {
                                layer.close(index);
                                layer.msg("删除失败！");
                            }
                        }
                    });
                });
            }
        });
        
    });

</script>
</body>
</html>